<template>
  <van-nav-bar left-text="返回" left-arrow @click-left="onClickLeft">
  </van-nav-bar>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="reppassword"
        type="password"
        name="reppassword"
        label="确认密码"
        placeholder="确认密码"
        :rules="[{ required: true, message: '请填写确认密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
import { useRoute, useRouter } from "vue-router";
import { ref, onMounted } from "vue";
const router = useRouter();
import { updatepwd } from "@/api/user";
function onClickLeft() {
  //返回上一页
  router.back();
}

const password = ref("");
const reppassword = ref("");

const onSubmit = (values) => {
  console.log("submit", values);
  if (password.value !== reppassword.value) {
    showNotify({
      type: "warning",
      message: "两次密码不一致",
    });
    return;
  }
  updatepwd({
    newpwd: password.value,
    newpwd1: reppassword.value,
  }).then((res) => {
    if (res.code == 0) {
      showNotify({
        type: "success",
        message: "密码修改成功",
      });
      localStorage.removeItem("username");
      localStorage.removeItem("ward");
      router.push("/login");
    } else {
      showNotify({
        type: "warning",
        message: res.msg,
      });
    }
  });
};
</script>

<style></style>
